"""
numpy基础： 数组示例
"""
import numpy as np


# 数组创建
def array_create():
    # 创建一个5元素数组
    my_array = np.array([1, 2, 3, 4, 5])
    print(my_array)
    print(my_array.shape)
    print(my_array[1:])
    print(my_array[:2])

    # 创建一个所有元素为0的数组
    zero_array = np.zeros(5)
    print(zero_array)

    # 创建一个所有元素为1的数组
    one_array = np.ones(5)
    print(one_array)
    print(one_array[2:])
    for i in one_array:
        print(i)

    # 创建一个随机数组
    random_array = np.random.random(5)
    print(random_array)
    int_random_array = np.random.randint(10, 100, 10)
    print(int_random_array)

    # 创建二维数组
    my_array_2d = np.array([[4, 6], [2, 3]])
    print(my_array_2d)
    print(my_array_2d.shape)

    # 创建全是0的二维数组
    zero_array_2d = np.zeros((2, 3), int)
    print(zero_array_2d)
    print(zero_array_2d[0][1])

    # 创建全是1的二维数组
    one_array_2d = np.ones((4, 5), int)
    print(one_array_2d)

    # 提取指定列
    my_array_2d_column_2 = my_array_2d[:, 1]
    print(my_array_2d_column_2)

    # 提取指定行
    my_array_2d_row_1 = my_array_2d[0, :]
    print(my_array_2d_row_1)


# 数组操作
def array_operate():
    a = np.array([[1.0, 2.0], [3.0, 4.0]])
    b = np.array([[5.0, 6.0], [7.0, 8.0]])
    print("a = \n", a)
    print("b = \n", b)
    # 元素相加
    sum = a + b
    # 元素相减
    difference = a - b
    # 元素相乘
    product = a * b
    # 元素相除
    quotient = a / b
    # 矩阵乘法
    matrix_product = a.dot(b)
    print("Sum = \n", sum)
    print("difference = \n", difference)
    print("product = \n", product)
    print("quotient = \n", quotient)
    print("Matrix_Product = \n", matrix_product)





# 程序入口
if __name__ == "__main__":
    # array_create()
    array_operate()
    pass